package com.celinio.readprops;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

/**
 * This servlet stores the properties values in the session and forwards the user to the result.jsp page
 * @author Celinio Fernandes
 *
 */
public class ReadPropertiesServlet extends HttpServlet {
	
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ReadPropertiesServlet.class);


	private static final long serialVersionUID = 1L;
	private static final String MAX_SIZE = "max_size";	
	private String size;
	
	
	private void readDatasourceConfig(long reloadInterval) {
	
		if(ReadExternalPropertiesUtil.initFile(reloadInterval)){			
			String sizeStr = ReadExternalPropertiesUtil.getProperty(MAX_SIZE);
			logger.info("Property : " + MAX_SIZE + " - value : " + sizeStr);			
			size = sizeStr;			
		}						
	}
		
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		logger.info("Calling doGet");	
		
		readDatasourceConfig(40000);	//  40000 ms ==> 40 s
		/*
		PrintWriter out = response.getWriter(); 
		out.println ( "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" 
				+ "<html> \n" + "<head> \n" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \n" 
				+ "<title> My first jsp </title> \n" + "</head> \n" + "<body> \n" +   "Size is : " + size +					
		"</font> \n" + "</body> \n" + "</html>" ); 
		*/
		
		HttpSession session = request.getSession(true);
        session.setAttribute("size", size);
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/result.jsp");
        dispatcher.forward(request, response);
	}

	
	 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
		 logger.info("Calling doPost");	
	    } 
}
